﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Photography.App_Data;

namespace Photography.Admin
{
    public partial class ManageGalleries : System.Web.UI.Page
    {
        protected new Photography.Admin.AdminMaster Master { get { return base.Master as Photography.Admin.AdminMaster; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Master.AdminID.HasValue)
                Response.Redirect("Default.aspx");

            if (!Page.IsPostBack)
            {
                cboCategory.SelectedValue = Master.CategoryId.ToString();
            }
        }

        protected void GetCategories(object sender, LinqDataSourceSelectEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
            e.Result = context.GetCategories();
        }

        protected void GetGalleries(object sender, LinqDataSourceSelectEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
            e.Result = context.GetGalleries(int.Parse(cboCategory.SelectedValue));
        }

        protected void GetPhotos(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (Master.GalleryId.HasValue)
            {
                Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
                System.Data.Linq.ISingleResult<Photography.App_Data.GetGalleryPhotosResult> data = context.GetGalleryPhotos(Master.GalleryId);
                e.Result = data;
            }
            else
                e.Result = new List<GetGalleryPhotosResult>();
        }

        protected void btnAddPhoto_Click(object sender, EventArgs e)
        {
            if (Master.GalleryId.HasValue)
                Master.RedirectToPhoto();
        }

        protected void btnAddGallery_Click(object sender, EventArgs e)
        {
            Master.RedirectToGallery(true);
        }

        protected void btnEditGallery_Click(object sender, EventArgs e)
        {
            Master.RedirectToGallery();
        }

        protected void grdPhotos_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

            int photoId = Convert.ToInt32(e.CommandArgument);
 
            if (e.CommandName == "DeletePhoto")
            {
                if (!Master.GalleryId.HasValue)
                {
                    context.UpdatePhoto(photoId, null, null, null, null, null, false, null);
                    lblNotification.Text = "Photo #" + photoId.ToString() + " was successfuly removed from slideshow";
                }
                else
                {
                    try
                    {
                        string location = MapPath((from a in context.GetGalleryPhotos(Master.GalleryId.Value) where a.ID == photoId select a.Location).First());
                        System.IO.File.Delete(location);
                    }
                    catch
                    {
                    }

                    context.DeletePhoto(photoId);
                    lblNotification.Text = "Photo #" + photoId.ToString() + " was successfuly deleted from gallery";
                }
            }
            else if (e.CommandName == "EditPhoto")
            {
                Master.RedirectToPhoto(photoId);
            }
            else if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
            {
                if (Master.GalleryId.HasValue)
                {
                    List<int> photos = new List<int>();
                    foreach (GetGalleryPhotosResult photo in context.GetGalleryPhotos(Master.GalleryId))
                        photos.Add(photo.ID);

                    int indexOfPhotoToMove = photos.IndexOf(photoId);
                    photos.RemoveAt(indexOfPhotoToMove);

                    if (e.CommandName == "MoveDown")
                    {
                        if (indexOfPhotoToMove == photos.Count)
                            photos.Insert(0, photoId);
                        else if (indexOfPhotoToMove + 1 == photos.Count)
                            photos.Add(photoId);
                        else
                            photos.Insert(indexOfPhotoToMove + 1, photoId);
                    }
                    else
                    {
                        if (indexOfPhotoToMove == 0)
                            photos.Add(photoId);
                        else
                            photos.Insert(indexOfPhotoToMove - 1, photoId);
                    }

                    for (int i = 0; i < photos.Count; i++)
                        context.UpdatePhoto(photos[i], null, null, i + 1, null, null, null, null);
                }
            }

            grdPhotos.DataBind();
        }

        protected void grdPhotos_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int photoId = 0;
                if (e.Row.DataItem is Photography.App_Data.GetGalleryPhotosResult)
                    photoId = (e.Row.DataItem as Photography.App_Data.GetGalleryPhotosResult).ID;
                else if  (e.Row.DataItem is Photography.App_Data.GetSlideShowPhotosResult)
                    photoId = (e.Row.DataItem as Photography.App_Data.GetSlideShowPhotosResult).ID;

                ((LinkButton)e.Row.Cells[4].Controls[0]).CommandArgument = photoId.ToString();
                ((LinkButton)e.Row.Cells[5].Controls[0]).CommandArgument = photoId.ToString();
                ((LinkButton)e.Row.Cells[6].Controls[0]).CommandArgument = photoId.ToString();
                ((LinkButton)e.Row.Cells[7].Controls[0]).CommandArgument = photoId.ToString();
            }
        }

        protected void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            Master.RedirectToManageGalleries(int.Parse(cboCategory.SelectedValue));
        }
    }
}